/**
 * 
 */
package mdb.com.vn.web.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import mdb.com.vn.model.Customer;
import mdb.com.vn.model.LuckyDraw;
import mdb.com.vn.model.LuckyDrawG1;
import mdb.com.vn.model.LuckyDrawG2;
import mdb.com.vn.model.Results;
import mdb.com.vn.services.MDBServices;
import mdb.com.vn.utils.TextUtil;

/**
 * @author tu.dod
 * 
 */

@ManagedBean(name = "resultBean")
@ViewScoped
public class ResultBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int luckyDrawNumber;
	private int checkSave;
	private int awards;

	private LuckyDraw luckyDraw;
	private LuckyDrawG1 luckyDrawG1;
	private LuckyDrawG2 luckyDrawG2;
	private Results results;
	private List<Results> listResults;

	private String msgResult;
	@ManagedProperty("#{mdbServices}")
	private MDBServices mdbServices;

	@PostConstruct
	public void init() {
		luckyDrawNumber = 0;
		checkSave = 0;
		msgResult = "";
		luckyDraw = new LuckyDraw();
		luckyDrawG1 = new LuckyDrawG1();
		luckyDrawG2 = new LuckyDrawG2();
		results = new Results();
		listResults = new ArrayList<Results>();
		listResults = mdbServices.getListResults();

	}

	public void getLuckyCustomer() {
		if (awards == 0) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Bạn chưa chọn giải thưởng nào ",
							"Vui lòng chọn giải thưởng"));
		} else {
			if (awards == 5) {
				luckyDrawG2 = mdbServices.getLuckyDrawG2ById(luckyDrawNumber);
				if (luckyDrawG2 == null) {
					FacesContext
							.getCurrentInstance()
							.addMessage(
									null,
									new FacesMessage(
											FacesMessage.SEVERITY_WARN,
											"Không có khách hàng nào trúng giải: "+TextUtil.getStringAwards(awards),
											""));
					results = new Results();
					checkSave = 0;
				} else {

					results.setCustomerId(luckyDrawG2.getCustomerId());
					Customer customer = mdbServices
							.getCustomerInfos(luckyDrawG2.getCustomerId());
					results.setCustomerName(customer.getCustomerName());
					results.setRefNo(luckyDrawG2.getRefNo());
					results.setAgroup(TextUtil.GROUP2);
					results.setAwards(TextUtil.getStringAwards(awards));
					results.setBranch(mdbServices.getCompanyNameByCode(customer
							.getBranchCode()));
					results.setLkrNumber(String.format("%05d", luckyDrawNumber));
					results.setCreatedBy("SYSTEM");
					results.setDateCreated(new Date());
					if (luckyDrawG2.getStatus().equals(TextUtil.PROCESSED)) {
						results.setStatus(TextUtil.PROCESSED);
						checkSave = 1;
						FacesContext.getCurrentInstance().addMessage(
								null,
								new FacesMessage(FacesMessage.SEVERITY_INFO,
										"CHÚC MỪNG KHÁCH HÀNG ĐÃ TRÚNG THƯỞNG",
										""));
					} else {
						results.setStatus(TextUtil.REJECTED);
						checkSave = 0;

						FacesContext
								.getCurrentInstance()
								.addMessage(
										null,
										new FacesMessage(
												FacesMessage.SEVERITY_ERROR,
												"KHÁCH HÀNG ĐÃ VI PHẠM ĐIỀU ĐOẢN THAM GIA KHUYẾN MÃI",
												""));
					}
				}
			} else {
				luckyDraw = mdbServices.getLuckyDrawById(luckyDrawNumber);
				if (luckyDraw == null) {
					luckyDrawG1 = mdbServices
							.getLuckyDrawG1ById(luckyDrawNumber);
					if (luckyDrawG1 == null) {
						FacesContext.getCurrentInstance().addMessage(
								null,
								new FacesMessage(FacesMessage.SEVERITY_WARN,
										"Không có khách hàng nào trúng giải: "+TextUtil.getStringAwards(awards),
										""));
						results = new Results();
						checkSave = 0;
					} else {

						results.setCustomerId(luckyDrawG1.getCustomerId());
						Customer customer = mdbServices
								.getCustomerInfos(luckyDrawG1.getCustomerId());
						results.setRefNo(luckyDrawG1.getRefNo());
						results.setCustomerName(customer.getCustomerName());
						results.setAgroup(TextUtil.GROUP1);
						results.setAwards(TextUtil.getStringAwards(awards));
						results.setBranch(mdbServices
								.getCompanyNameByCode(customer.getBranchCode()));
						results.setLkrNumber(String.format("%05d",
								luckyDrawNumber));
						results.setCreatedBy("SYSTEM");
						results.setDateCreated(new Date());
						if (luckyDrawG1.getStatus().equals(TextUtil.PROCESSED)) {
							results.setStatus(TextUtil.PROCESSED);
							checkSave = 1;

							FacesContext
									.getCurrentInstance()
									.addMessage(
											null,
											new FacesMessage(
													FacesMessage.SEVERITY_INFO,
													"CHÚC MỪNG KHÁCH HÀNG ĐÃ TRÚNG THƯỞNG",
													""));
						} else {
							results.setStatus(TextUtil.REJECTED);
							checkSave = 0;

							FacesContext
									.getCurrentInstance()
									.addMessage(
											null,
											new FacesMessage(
													FacesMessage.SEVERITY_ERROR,
													"KHÁCH HÀNG ĐÃ VI PHẠM ĐIỀU ĐOẢN THAM GIA KHUYẾN MÃI",
													""));
						}
					}
				} else {
					results.setCustomerId(luckyDraw.getCustomerRef());
					Customer customer = mdbServices.getCustomerInfos(luckyDraw
							.getCustomerRef());
					results.setRefNo(luckyDraw.getTransRef());
					results.setCustomerName(customer.getCustomerName());
					results.setAgroup(TextUtil.GROUP1);
					results.setAwards(TextUtil.getStringAwards(awards));
					results.setBranch(mdbServices.getCompanyNameByCode(customer
							.getBranchCode()));
					results.setLkrNumber(String.format("%05d", luckyDrawNumber));
					results.setCreatedBy("SYSTEM");
					results.setDateCreated(new Date());
					results.setStatus(TextUtil.PROCESSED);
					checkSave = 1;

					FacesContext
							.getCurrentInstance()
							.addMessage(
									null,
									new FacesMessage(
											FacesMessage.SEVERITY_INFO,
											"CHÚC MỪNG KHÁCH HÀNG ĐÃ TRÚNG THƯỞNG",
											""));
				}
			}
		}

	}

	public void saveLuckyCustomer() {
		if (results != null && results.getStatus().equals(TextUtil.PROCESSED)) {
			try {
				mdbServices.addResult(results);
				results = new Results();
				FacesContext.getCurrentInstance().addMessage(
						null,
						new FacesMessage(FacesMessage.SEVERITY_INFO,
								"Đã lưu thành công", ""));
				listResults = mdbServices.getListResults();
				checkSave = 0;
			} catch (Exception e) {
				FacesContext
						.getCurrentInstance()
						.addMessage(
								null,
								new FacesMessage(
										FacesMessage.SEVERITY_ERROR,
										"Lỗi! không lưu được thông tin khách hàng trúng thưởng",
										""));
				checkSave = 0;
			}
		}
	}

	public void setMdbServices(MDBServices mdbServices) {
		this.mdbServices = mdbServices;
	}

	/**
	 * @return the luckyDrawNumber
	 */
	public int getLuckyDrawNumber() {
		return luckyDrawNumber;
	}

	/**
	 * @return the luckyDraw
	 */
	public LuckyDraw getLuckyDraw() {
		return luckyDraw;
	}

	/**
	 * @param luckyDraw
	 *            the luckyDraw to set
	 */
	public void setLuckyDraw(LuckyDraw luckyDraw) {
		this.luckyDraw = luckyDraw;
	}

	/**
	 * @return the luckyDrawG1
	 */
	public LuckyDrawG1 getLuckyDrawG1() {
		return luckyDrawG1;
	}

	/**
	 * @param luckyDrawG1
	 *            the luckyDrawG1 to set
	 */
	public void setLuckyDrawG1(LuckyDrawG1 luckyDrawG1) {
		this.luckyDrawG1 = luckyDrawG1;
	}

	/**
	 * @param luckyDrawNumber
	 *            the luckyDrawNumber to set
	 */
	public void setLuckyDrawNumber(int luckyDrawNumber) {
		this.luckyDrawNumber = luckyDrawNumber;
	}

	/**
	 * @return the msgResult
	 */
	public String getMsgResult() {
		return msgResult;
	}

	/**
	 * @param msgResult
	 *            the msgResult to set
	 */
	public void setMsgResult(String msgResult) {
		this.msgResult = msgResult;
	}

	/**
	 * @return the luckyDrawG2
	 */
	public LuckyDrawG2 getLuckyDrawG2() {
		return luckyDrawG2;
	}

	/**
	 * @param luckyDrawG2
	 *            the luckyDrawG2 to set
	 */
	public void setLuckyDrawG2(LuckyDrawG2 luckyDrawG2) {
		this.luckyDrawG2 = luckyDrawG2;
	}

	/**
	 * @return the results
	 */
	public Results getResults() {
		return results;
	}

	/**
	 * @param results
	 *            the results to set
	 */
	public void setResults(Results results) {
		this.results = results;
	}

	/**
	 * @return the checkSave
	 */
	public int getCheckSave() {
		return checkSave;
	}

	/**
	 * @param checkSave
	 *            the checkSave to set
	 */
	public void setCheckSave(int checkSave) {
		this.checkSave = checkSave;
	}

	/**
	 * @return the awards
	 */
	public int getAwards() {
		return awards;
	}

	/**
	 * @param awards
	 *            the awards to set
	 */
	public void setAwards(int awards) {
		this.awards = awards;
	}

	/**
	 * @return the listResults
	 */
	public List<Results> getListResults() {
		return listResults;
	}

	/**
	 * @param listResults
	 *            the listResults to set
	 */
	public void setListResults(List<Results> listResults) {
		this.listResults = listResults;
	}

}
